package cn.liutao.dish.api;

import cn.liutao.common.entity.Validation;
import cn.liutao.common.utils.ServletUtils;
import cn.liutao.common.utils.ValidUtil;
import cn.liutao.dish.entity.po.Cuisine;
import cn.liutao.dish.entity.po.Taste;
import cn.liutao.dish.service.CuisineService;
import cn.liutao.dish.service.TasteService;
import com.mybatisflex.core.paginate.Page;
import lombok.extern.slf4j.Slf4j;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author liutao
 * @Description 口味Api
 * @date 2023/10/31 17:03
 * @Version 1.0
 */
@WebServlet("/taste")
@Slf4j
public class TasteServlet extends HttpServlet {
    private TasteService tasteService;

    @Override
    public void init() throws ServletException {
        tasteService = TasteService.getInstance();
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)  {
        Taste taste = ServletUtils.getQueryParam(req, Taste.class);
        Page<Taste> page  = tasteService.selectByList(taste);
        ServletUtils.ok(resp,page);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) {
        Taste taste = ServletUtils.getBodyParam(req, Taste.class);
        ValidUtil.validated(taste,Validation.AddGroup.class);
        tasteService.insert(taste);
        ServletUtils.ok(resp,"添加成功");
    }

    @Override
    protected void doPut(HttpServletRequest req, HttpServletResponse resp) {
        Taste taste = ServletUtils.getBodyParam(req, Taste.class);
        ValidUtil.validated(taste,Validation.EditGroup.class);
        tasteService.updateById(taste);
        ServletUtils.ok(resp,"修改成功");
    }

    @Override
    protected void doDelete(HttpServletRequest req, HttpServletResponse resp)  {
        List<String> ids = ServletUtils.getQueryParams(req, "id");
        for (String id : ids) {
            tasteService.deleteById(Integer.valueOf(id));
        }
        ServletUtils.ok(resp,"删除成功");
    }
}
